package pl.koziolekweb.szkola.NKBeans;

// NK-Beans/Lights.java
 
import javax.swing.*;
import java.beans.*;
import java.awt.*;

  // widok modelu Kibice
public class Lights extends JLabel 
        implements PropertyChangeListener {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Color color = Color.GREEN;
    private Dimension size = new Dimension(25,25);
    
    public Lights() {
        setOpaque(true);
        setBackground(color);
    }
    
    // wywolywana gdy zmieni sie model
    public void propertyChange(PropertyChangeEvent e) {
        int[] kibice = (int[])e.getNewValue();
        double ratio;
        if (kibice[0]+kibice[1] <= Props.getIntProp("limit"))
            ratio = 0;
        else
            ratio = Math.abs(kibice[0]-kibice[1])/
                    (double)(kibice[0]+kibice[1]);
        
        if      (ratio <= Props.getDoubleProp("greenLight"))
            color = Color.GREEN;
        else if (ratio <= Props.getDoubleProp("yellowLight"))
            color = Color.YELLOW;
        else
            color = Color.RED;
        
        setBackground(color);
        repaint();
    }
    
    public Dimension getMinimumSize() {
        return size;
    }

    public Dimension getPreferredSize() {
        return size;
    }
}
